//
//  AppDelegate.m
//  VEMoneyPorject
//
//  Created by 陈建 on 2018/9/4.
//  Copyright © 2018年 陈建. All rights reserved.
//

#import "AppDelegate.h"

#import "CJAppVersionTool.h"
#import "CJRootVCSelectionTool.h"
#import "CJUserInfoManager.h"
#import "WelcomeVC.h"
#import "WXApi.h"
#import "WXApiManager.h"
static NSString *weixinAppID = @"wxd70f5ae8d643cac4";
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = kWhiteColor;
    
   
    // Override point for customization after application launch.
    
    [self setRootViewControllerForKeyWindow];
    [self.window makeKeyAndVisible];
    [self setupRegisterWeixinApp];
    
    return YES;
}
-(void)setupRegisterWeixinApp{
    [WXApi registerApp:weixinAppID];
    
}

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}

// NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
#pragma mark - 设置根控制器
- (void)setRootViewControllerForKeyWindow{
    // 1. 获取当前版本信息
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // 当前应用软件版本  比如：1.0.1
    NSString *currentBundleVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"%@====%@",currentBundleVersion,[CJAppVersionTool savedAppVersion]);
    // 2. 判断当前版本是否是最新
    if ([currentBundleVersion isEqualToString:[CJAppVersionTool savedAppVersion]]) {    // 3.1 如果不是新版本
        
        // 由授权控制器决定窗口的根控制器
        [CJRootVCSelectionTool setRootViewControllerForWindow:self.window];
        
    } else {                                            // 3.2 如果是新版本
        
        [[CJUserInfoManager sharedManager] didLoginOut];
        
        // 1. 保存新版本信息（偏好设置)
        [CJAppVersionTool saveNewAppVersion:currentBundleVersion];
        
        // 2. 创建新特性画面控制器collectionViewController
        WelcomeVC *newFeatureVC = [[WelcomeVC alloc] init];
        // 设置窗口的根控制器
        self.window.rootViewController = newFeatureVC;
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
